from qiniu import Auth, put_file, etag
import uuid
access_key = 'OawWER_mnAafSQnj3zpad_bduDckFy1GYZYgsAvy'
secret_key = 'KMSDNu0yssOyjyXKYIEsfuZFZGcpbJUWVekAVyor'
from aijia.settings import BASE_DIR


def qiniu_token(bucked_name):

    q = Auth(access_key=access_key,
             secret_key=secret_key)

    token = q.upload_token(bucked_name,expires=36000)

    return token


def upload_img(bucked_name, file_path, domain_name):
    """
    收集本地信息到云服务器上
    参考地址：https://developer.qiniu.com/kodo/sdk/1242/python
    """
    # 指定上传空间，获取token
    token = qiniu_token(bucked_name)
    # 指定图片名称
    file_name = '{}'.format(uuid.uuid4())
    ret, info = put_file(token,  file_name, file_path)
    image_code = ret.get('key')
    img_url = domain_name + image_code

    return img_url,image_code
if __name__ == '__main__':
    bucked_name = 'fanxinde'
    file_path = r'/home/ubuntu/Desktop/123.png'
    domain_name = 'http://qsw9kltom.hn-bkt.clouddn.com/'
    print(upload_img(bucked_name, file_path, domain_name))

